extends Control

@export var max_value: float = 100
@export var current_value: float  = 50
@export var danger_percent: float = 0.35:
	get:
		return danger_percent
	set(value):
		hp_color.set_offset(1,danger_percent)
		hp_color.set_offset(2,danger_percent)
		
# 血条的颜色跟随血量的百分比变化 
@export var hp_color:Gradient

# 资源类型创建导出变量 就可以在编辑器中设置资源
@export var UIEVENT:UIEvent

# 动态的更改资源 因此需要加载此资源 不用担心，这个资源依然是引用的,因此所有更改都会生效
var current_hp_style:StyleBoxFlat = preload("res://font/hp_front_panel_theme.tres")

var percent:float = float(current_value) / max_value


func _ready():
	print("UIEVENT = ",UIEVENT)
	UIEVENT.hp_info_changed.connect(update_ui)
	hp_color.set_offset(1,danger_percent)
	hp_color.set_offset(2,danger_percent)
	print("connect update hp")
	pass
	
func update_ui(current:float, total: float):
	#print("update ui current = " , current, " max = ", total)
	max_value = total
	current_value = current
	var color = hp_color.sample(current_value / max_value)
	current_hp_style.bg_color = color
	

func _process(_delta):
	percent = current_value / max_value
	%HpNumber.set_text("%s/%s"%[current_value,max_value])
	var c =  %CurrentHp.get_size()
	var t =  %HpTotal.get_size().x
	c = Vector2(int(t * percent), c.y)
	%CurrentHp.set_size(c)
	
	pass
	
